home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_round.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  2.0 KB  |  63 lines

  1. /* Copyrighted Pixar 1989 */
  2. /* From the RenderMan Companion p.364 */
  3. /* Listing 16.23  Displacement shader for bevelling perpendicular bilinear patches*/
  4.  
  5. /* 
  6.  * round(): displace the edge of a bilinear patch so that, if it is placed 
  7.  * next to another patch at a right angle, the edge will be rounded.
  8.  */
  9. displacement
  10. k3d_round (float radius = .10 )
  11. {
  12.     float     uu,    /* distance in u to the nearest "vertical" edge */
  13.          vv,    /* distance in v to the nearest "horizontal" edge */
  14.          lu,    /* "real" distance to the nearest "vertical" edge */
  15.          lv;    /* "real" distance to the nearest "horizontal" edge */
  16.     point     center,/* point toward which the surface is displaced     */
  17.          dpdu,    /* dPdu pointed toward patch center line     */
  18.          dpdv;    /* dPdv pointed toward patch center line     */
  19.  
  20.     /* Find the distance in parameter space from the nearest edge in
  21.        u and in v, and the directions away from those edges. */
  22.     if (u < .5) {
  23.         uu = u;
  24.         dpdu = dPdu;
  25.     } else {
  26.         uu = 1 - u;
  27.         dpdu = -dPdu;
  28.     }
  29.     if (v < .5) {
  30.         vv = v;
  31.         dpdv = dPdv;
  32.     } else {
  33.         vv = 1 - v;
  34.         dpdv = -dPdv;
  35.     }
  36.  
  37.     /* Find the distances from the edges in the current space. */
  38.     lu = length(dPdu*uu);
  39.     lv = length(dPdv*vv);
  40.  
  41.     if (lu < radius || lv < radius) {    /* only if within radius of 
  42.                                                          an edge...  */
  43.     /*
  44.      * Find the point towards which the surface  point will be 
  45.      *  moved. This center is on the center line of a cylinder, if we 
  46.      *  are not near the corner of the patch, or is the center of a 
  47.      *  sphere, if we are. We move `center' to the nearest inflection 
  48.      *  edge along u and/or v.
  49.      */
  50.         center = point(0,0,0);
  51.         if (lu < radius)
  52.             center = (radius-lu) * normalize(dpdu);
  53.         if (lv < radius)
  54.             center += (radius-lv) * normalize(dpdv);
  55.         /* Move center perpendicular to the surface */
  56.         center += P - radius*normalize(N);
  57.         /* Make P be distance 'radius' along the line 
  58.                  * from 'center' to P */
  59.         P = center + radius*normalize( P-center );
  60.     }
  61.     N = calculatenormal(P);
  62. }
  63.